
<!-- 计量单位 -->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['saas:unit:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="unitList">
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          <div>{{ row.status === 1 ? "禁用" : "启用" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-hasPermi="['saas:unit:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            v-hasPermi="['saas:unit:edit']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px" :show-message="false">
        <el-form-item label="单位名称" prop="unit" required>
          <el-input
            v-model="form.unit"
            :maxlength="10"
            placeholder="请输入单位名称,十个字以内"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status" v-if="form.unitId">
          <el-switch
            v-model="form.status"
            active-text="启用"
            :active-value="0"
            inactive-text="禁用"
            inactive-color="#F56C6C"
            :inactive-value="1"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDataList, addData, upData } from "@/api/system/unit.js";
export default {
  name: "Unit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 999,
        unit: "",
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      unitList: [],
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getDataList(this.queryParams).then((res) => {
        this.unitList = res.rows;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        unit: "",
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改单位";
      this.form = {
        unitId: row.unitId,
        unit: row.unit,
        status: row.status,
      };
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("是否确认删除该单位？", "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.form = {
          unitId: row.unitId,
          status: 2, // 0正常 1禁用 2删除
        };
        upData(this.form).then((res) => {
          this.getList();
          this.$message.success("删除成功");
        });
      });
    },
    // 添加| 修改 单位
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.unitId) {
            upData(this.form).then(() => {
              this.$message.success("修改成功");
              this.cancel();
              this.getList();
            });
          } else {
            addData(this.form).then(() => {
              this.$message.success("新增成功");
              this.cancel();
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
